package com.liuxinlong.modules.system.service.impl;

import com.liuxinlong.common.ExcelImportCallBack;
import com.liuxinlong.common.ExcelImportService;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.RowError;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.dao.RoleDao;
import com.liuxinlong.modules.entity.UserRole;
import com.liuxinlong.modules.system.service.RoleService;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户角色信息服务实现
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-01-12
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDao roleDao;

	@Autowired
	private ExcelImportService excelImportService;

	@Override
	public List<Map<String, Object>> pageUserRoleInfo(Map<String, Object> queryParam) {
		List<UserRole> originList = roleDao.pageRoleList(queryParam);
		List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
		int sort = (int) queryParam.get("startNum") + 1;
		for (UserRole role : originList) {
			Map<String, Object> map = ObjectUtils.object2Map(role);
			map.put("sort", sort);
			resultList.add(map);
			sort++;
		}
		return resultList;
	}

	@Override
	public int getUserRoleCount(Map<String, Object> queryParam) {
		return roleDao.getRoleCount(queryParam);
	}

	@Override
	public void addUserRoleInfo(UserRole userRole) {
		userRole.setId(SnowFlake.nextIdStr());
		roleDao.addRole(userRole);
	}

	@Override
	public void updateUserRoleInfo(UserRole userRole) {
		roleDao.updateRole(userRole);
	}

	@Override
	public void deleteUserRoleInfo(String id) {
		roleDao.deleteRole(id);
	}

	@Override
	public ImportResp batchAddRole(List<UserRole> dataList) {
		for (UserRole userRole : dataList) {
			userRole.setId(SnowFlake.nextIdStr());
			roleDao.insert(userRole);
		}
		ImportResp resp = new ImportResp();
		resp.setRetCode(ReturnCodes.HTTP_SUCCESS.getRetCode());
		resp.setRetMessage("导入角色信息成功");
		return resp;
	}

	@Override
	public ImportResp importRoles(String fileName) {
		String filePath = fileName;
		return excelImportService.excelFile(filePath, 2, new ExcelImportCallBack() {
			@Override
			public Collection<RowError> checkRow(String[] record) {
				return checkRecordValid(record);
			}

			@Override
			public boolean persistRowToDb(String[] record) {
				UserRole userRole = covertToBean(record);
				return false;
//				try {
//					UserDepartmentDao.addDetail(UserDepartment);
//					return true;
//				} catch (Exception e) {
//					return false;
//				}
			}

			@Override
			public ImportResp partFailure(ImportResp resp) {
				resp.setRetMessage("导入角色数据部分失败，总数=" + resp.getTotal() + ",失败总数=" + resp.getFailedTotal());
				return resp;
			}

			@Override
			public ImportResp allSuccess(ImportResp resp) {
				resp.setRetMessage("导入角色数据全部成功，总数=" + resp.getTotal());
				return resp;
			}

			@Override
			public List<Map> convertFailList(List<String[]> failRecords) {
				List<Map> list = new ArrayList<>();
				int sort = 1;
				for (String[] failRecord : failRecords) {
					Map item = new HashMap<>();
					item.put("sort", sort);
					item.put("name", failRecord[0]);
					item.put("sn", failRecord[1]);
					item.put("remark", failRecord[2]);
					list.add(item);
					sort++;
				}
				return list;
			}

		});
	}

	/**
	 * 转换数据为入库数据
	 *
	 * @param record 数据记录
	 * @return 入库数据
	 */
	private UserRole covertToBean(String[] record) {
		UserRole userRole = new UserRole();
		userRole.setName(record[0].trim());
		userRole.setSn(record[1].trim());
		userRole.setRemark(record[2].trim());
		return userRole;

	}

	/**
	 * 校验参数合法性
	 *
	 * @param record 导入数据
	 * @return 参数合法性
	 */
	private Collection<RowError> checkRecordValid(String[] record) {
		List<RowError> rowErrors = new ArrayList<>();
		if (StringUtils.isEmpty(record[0].trim())) {
			addRowError("角色名称不能为空", rowErrors);
		} else {
			if (record[0].trim().length() > 50) {
				addRowError("角色名称长度不能超过20", rowErrors);
			}
		}
		if (StringUtils.isEmpty(record[1].trim())) {
			addRowError("角色编码不能为空", rowErrors);
		} else {
			if (record[1].trim().length() > 20) {
				addRowError("角色编码长度不能超过20", rowErrors);
			}
		}
		if (!StringUtils.isEmpty(record[2].trim())) {
			if (record[2].trim().length() > 200) {
				addRowError("描述长度不能超过200", rowErrors);
			}
		}

		return rowErrors;
	}

	/**
	 * 添加错误行数据
	 *
	 * @param errorMessage 错误信息
	 * @param rowErrors    错误行数据
	 */
	private void addRowError(String errorMessage, List<RowError> rowErrors) {
		RowError rowError = new RowError(errorMessage);
		rowErrors.add(rowError);
	}

}
